@tool
class_name PursueContextBehavior
extends BaseContextBehavior

#region 属性
@export var target_reached_threshold:float = 0.5
@export var slow_radius:float = 2
@export var prediction_t:float = 0
var _reached_last_target:bool = true
var _target_pos_cached:Vector2
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_context(steering_component:SteeringComponent,resolution_radio:int):
	super.init_context(steering_component,resolution_radio)
func get_interest(entity:CharacterEntity,directions:Array[Vector2]) -> Array[float]:
	interest = []
	interest.resize(directions.size())
	var detector = entity.get_detector(GameEnum.DetectorType.Target)
	var closed_visible_entity = detector.get_closed_entity(GameEnum.TargetDetectorTargetType.VisibleTarget,global_position) if detector else null
	if _reached_last_target:
		if not closed_visible_entity:
			return interest
		else:
			_reached_last_target = false
			_target_pos_cached = closed_visible_entity.get_pos() + closed_visible_entity.get_vel() * prediction_t * (closed_visible_entity.get_pos() - global_position).length()/steering_component.max_speed
	if (_target_pos_cached - global_position).length() < target_reached_threshold:
		_reached_last_target = true
		return interest
	var target_vec:Vector2 = _target_pos_cached - global_position
	var target_dir:Vector2 = target_vec.normalized()
	var is_slow = target_vec.length() < slow_radius
	for i in range(directions.size()):
		interest[i] = directions[i].dot(target_dir) * (target_vec.length()/slow_radius if is_slow else 1) * sensitive
	return interest
func get_danger(entity:CharacterEntity,directions:Array[Vector2]) -> Array[float]:
	danger = []
	danger.resize(directions.size())
	return danger
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _draw() -> void:
	super._draw()
#endregion
